package model

import (
	"fmt"
	"gowork/shoppingBodyAward/utils"
	"time"
)

type CallRecord struct { 
	CallRecordId int64 `xorm:"pk autoincr"`
	CustomerId int64
	CustomerName string
	AccountId int64
	AccountName string
	CallType int
	CallState int
	CallTime int
	CreatedAt time.Time
  	UpdatedAt time.Time
}

func (cr *CallRecord)InsertCallRecord(lktv *CallRecord) map[string]string{
	x.Insert(lktv)
	var ts *LinkageTask
	var t LinkageTask
	var lkfs *LinkageFailureSucces

	w := new(utils.Worker)

	x.SQL("select * from where account_id = ? and customer_id = ? and term_of_validity >= ? and is_over = 1", lktv.AccountId, lktv.CustomerId, time.Now().Format("2006-01-02 15:04:05")).Get(&t)

	//x.Where("term_of_validity >= ?", time.Now().Format("2006-01-02 15:04:05")).And("is_over = ?", 1).
	//	And("account_id = ?", lktv.AccountId).
	//	And("customer_id = ?", lktv.CustomerId).
	//	Get(&t)
	if t.LinkageTaskId != 0 && lktv.CallTime >= 60{
		t.IsOver = 1
		ts.UpdateTermOfValidity(&t)

		var newLkfs = LinkageFailureSucces{
			LinkageFailureSuccesId: w.GetId(),
			FailureSuccesState : 0,
			CustomerId : t.CustomerId,
			CustomerName : t.CustomerName,
			LinkageTaskId : t.LinkageTaskId,
			CreatedAt : time.Now(),
			UpdatedAt : time.Now(),
		}
		lkfs.InsertLinkageFailureSucces(&newLkfs)
	}
	return map[string]string{"message": "成功","code":"200"}
}

func (cr *CallRecord)QueryCallRecord(cId int64, aId int64, mint time.Time, maxt time.Time) []*CallRecord{
	var crListOne []*CallRecord

	x.SQL("select * from call_record where customer_id = ? and account_id = ? and call_time >= 60 and created_at >= ? and created_at <= ?", cId, aId, mint.Format("2006-01-02 15:04:05"), maxt.Format("2006-01-02 15:04:05")).Find(&crListOne)

	fmt.Println(crListOne)
	return crListOne
}

//func (cr *CallRecord)QueryCallRecordTwo(cId int64,aId int64, mint time.Time, maxt time.Time) []*CallRecord{
//	var crListTwo []*CallRecord
//
//	x.Where("account_id = ?", aId).And("customer_id = ?", cId).
//		And("call_time >= ?", 60).
//		And("created_at >= ?",  mint.Format("2006-01-02 15:04:05")).
//		And("created_at <= ?", maxt.Format("2006-01-02 15:04:05")).Find(&crListTwo)
//	return crListTwo
//}

func (cr *CallRecord)QueryCallRecordByBool(cId int64) bool{
	var crList []*CallRecord
	x.Join("INNER","account", "account.account_id = call_record.account_id").
		Where("call_record.created_at >= ?", time.Now().AddDate(0,0,-4).Format("2006-01-02 15:04:05")).
		And("call_record.call_state = ?", 2).
		And("call_record.call_type = ?", 1).
		And("call_record.customer_id = ?", cId).And("account.line_type = ?", 2).
		Desc("call_record.created_at").
		Find(&crList)

	fmt.Println(len(crList))
	if len(crList) >= 4{
		return true
	}
	return false
} 